package com.example.controller;

import com.example.entity.TestUser;
import com.example.service.UserService;
import com.example.vo.Author;
import com.example.vo.Book;
import com.example.vo.VUser;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class HelloController {

    @Resource
    private UserService userService;

    @RequestMapping("expression")
    public String index(Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        HttpSession session2 = request.getSession();
        System.out.println(session2 == session);

        model.addAttribute("author", "alex");

        Book book = new Book();
        Author author = new Author();
        author.setName("lucy");
        book.setAuthor(author);
        model.addAttribute("book", book);
        // model.addAttribute("book.author.name", "lucy");

        String[] books1 = {"红楼梦", "西游记", "三国演义", "水浒传"};
        List<String> books2 = Arrays.asList(books1);
        model.addAttribute("books1", books1);
        model.addAttribute("books2", books2);

        HashMap<String, Object> user = new HashMap<>();
        user.put("name", "alex");
        user.put("age", 12);
        user.put("isHandsome", true);
        model.addAttribute("user", user);

        model.addAttribute("flag", true);
        model.addAttribute("flag1", true);
        model.addAttribute("flag2", false);

        model.addAttribute("thId", "box01");

        VUser user1 = new VUser("root", "123");
        model.addAttribute("user1", user1);

        return "expression";
    }

    @RequestMapping("aaa")
    @ResponseBody
    public String aaa(String name) {
        name = name == null ? "NULL":name;
        return name;
    }

    @RequestMapping("aaa/bbb")
    @ResponseBody
    public String bbb() {
        return "bbb";
    }

    @RequestMapping("test")
    public String test01(Model model) {
        TestUser user = new TestUser();
        user.setUsername("jack");
        user.setPassword("112233");
        user.setHobbies(Arrays.asList(new String[]{"singing", "dancing", "football"}));
        Map<String, String> maps = new HashMap<>();
        maps.put("1", "o");
        maps.put("2", "g");
        maps.put("3", "a");
        maps.put("4", "j");
        user.setSecrets(maps);
        model.addAttribute("user", user);
        return "test";
    }

    @RequestMapping("user")
    public String findAll(Model model) {
        model.addAttribute("ruser", userService.selectByPrimaryKey(1001));
        return "user";
    }
}
